package com.neo.community.commons.util;

import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 封装http方法
 * 1.get
 * 2.post
 * 3. ...
 */
public class HttpUtils {

    public static final Gson gson = new Gson();

    /**
     * get方法
     * @param url
     * @return
     */
    public static Map<String, Object> doGet(String url) {

        Map<String, Object> map = new HashMap<>();

        CloseableHttpClient httpClient = HttpClients.createDefault();
        /**
         * 设置超时时间
         */
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000) //请求超时
                .setSocketTimeout(5000)    //底层超时
                .setRedirectsEnabled(true)  //允许自动重定向
                .build();   //构建
        /**
         *获取 第三方平台返回的 url
         */
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);


        /**
         * 发送请求
         * 发送请求时一般使用try catch捕获信息
         * 如果对性能有要求就不要打印堆栈 e.printStackTrace();因为测试完后发生异常的概率很低
         */
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            /**
             * 请求成功
             * 一般返回值 200 为成功
             */
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String jsonResult = EntityUtils.toString(httpResponse.getEntity());
                map = gson.fromJson(jsonResult, map.getClass());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            /**
             * 关闭请求
             */
            try {
                httpClient.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 封装post
     * @return
     */
    public static String doPost(String url, String data, int timeout) {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        /**
         * 超时设置
         */
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout) //请求超时
                .setSocketTimeout(timeout)    //底层超时
                .setRedirectsEnabled(true)  //允许自动重定向
                .build();   //构建

        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Context-Type", "text/html; charset=UTF-8");
        /**
         * 使用字符串传参
         */
        if (data != null && data instanceof String) {
            StringEntity stringEntity = new StringEntity(data, "UTF-8");
            httpPost.setEntity(stringEntity);
        }
        /**
         * 发送请求
         */
        try {
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(httpResponse.getEntity());
                return result;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                httpClient.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  null;
    }
}
